/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.fast.web.FastController;
import com.vtuzx.kingdee.xqc_api.service.IXqcIfService;
import com.vtuzx.kingdee.xqc_api.service.IXqcInventoryService;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurchaseService;
import com.vtuzx.web.service.IVtuzxWebService;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 外部接口
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/if/")
@RestController
public class XqcIfController {

    /** 接口服务 */
    @Autowired
    private IXqcIfService ifService;

    @Autowired
    private IXqcInventoryService inventoryService;

    /**
     * 存追溯码履历
     */
    @GetMapping("/traceability_history")
    public ModelAndView traceabilityHistory(@RequestParam Map<String, Object> p, HttpServletRequest req) throws VtuzxException {
        return ifService.traceabilityHistory(p, req);
    }

    /**
     *
     */
    @PostMapping("/get_traceability_history_list")
    @VtuzxApi
    public Object getTraceabilityHistoryList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return ifService.getTraceabilityHistoryList(_userId, name, param);
    }

    /**
     * 箱码(托盘)绑定RFID
     */
    @PostMapping("/bind_box_rfid")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "box_no", label = "箱号", notNull = true),
            @VtuzxValidator(key = "rfid", label = "RFID", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object bindBoxRfid(@RequestBody Map<String, Object> param) throws VtuzxException {
        String boxNoKey = VtuzxUtil.getString(param, "box_no");
        String boxNo;
        if (boxNoKey.contains("|;")) {
            boxNo = boxNoKey.substring(boxNoKey.lastIndexOf("|;") + 2);
        } else {
            boxNo = boxNoKey;
        }
        String rfid = VtuzxUtil.getString(param, "rfid");
        long timestamp = VtuzxUtil.getLong(param, "timestamp");
        String sign = VtuzxUtil.getString(param, "sign");
        boolean checkSignOnly = VtuzxUtil.getBoolean(param, "cso");
        ifService.bindBoxRfid(boxNo, rfid, timestamp, sign, checkSignOnly);
        return null;
    }

    /**
     * RFID通过履历
     */
    @PostMapping("/record_rfid_inout")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "type", label = "类型", notNull = true, options = { "in", "out" }),
            @VtuzxValidator(key = "rfids", label = "RFID", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object recordRfidInOut(@RequestBody Map<String, Object> param) throws VtuzxException {
        String type = VtuzxUtil.getString(param, "type");
        List<String> rfids = VtuzxUtil.getObject(param, "rfids");
        String palletRfid = VtuzxUtil.getString(param, "pallet_rfid");
        long timestamp = VtuzxUtil.getLong(param, "timestamp");
        String sign = VtuzxUtil.getString(param, "sign");
        boolean checkSignOnly = VtuzxUtil.getBoolean(param, "cso");
        ifService.recordRfidInOut(type, palletRfid, rfids, timestamp, sign, checkSignOnly);
        return null;
    }

    /**
     * 根据箱号获取物料
     */
    @PostMapping("/get_material_by_box")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "box_no", label = "箱号", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object getMaterialByBox(@RequestBody Map<String, Object> param) throws VtuzxException {
        String boxNo = VtuzxUtil.getString(param, "box_no");
        long timestamp = VtuzxUtil.getLong(param, "timestamp");
        String sign = VtuzxUtil.getString(param, "sign");
        boolean checkSignOnly = VtuzxUtil.getBoolean(param, "cso");
        return ifService.getMaterialByBox(boxNo, timestamp, sign, checkSignOnly);
    }


    /**
     * 托盘盘点
     */
    @PostMapping("/tray_inventory")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "rfid_list", label = "rfid", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object trayInventory(@RequestBody Map<String, Object> param) throws VtuzxException {
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid_list");
        long timestamp = VtuzxUtil.getLong(param, "timestamp");
        String sign = VtuzxUtil.getString(param, "sign");
        String ip = VtuzxUtil.getString(param, "ip");
        boolean checkSignOnly = VtuzxUtil.getBoolean(param, "cso");
        boolean highlight = VtuzxUtil.getBoolean(param,"highlight");
        ifService.trayInventory(rfidList, timestamp, sign, ip, checkSignOnly,highlight);
        return null;
    }


    /**
     * 箱子绑定托盘
     */
    @PostMapping("/boxNo_bind_tray")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "rfid_list", label = "rfid", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object boxNoBindTray(@RequestBody Map<String, Object> param) throws VtuzxException {
        ifService.boxNoBindTray(param);
        return null;
    }

    /**
     * 箱子绑定追溯码
     */
    @PostMapping("/boxNo_bind_trace_back")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "key", label = "key", notNull = true),
            @VtuzxValidator(key = "trace_back", label = "traceBack", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object boxNoBindTraceBack(@RequestBody Map<String, Object> param) throws VtuzxException {
        ifService.boxNoBindTraceBack(param);
        return null;
    }

    /**
     * 修改5号库数据
     */
    @PostMapping("/update_stock_data")
    @VtuzxApi
    public Object updateStockData(@RequestBody Map<String, Object> param) throws VtuzxException {
        ifService.updateStockData(param);
        return null;
    }

    /**
     * 修改查询未打印的箱码状态
     */
    @PostMapping("/set_noPrint_boxNo_flag")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "print_flag", label = "状态", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object setNoPrintBoxNoFlag(@RequestBody Map<String, Object> param) throws VtuzxException {
        return ifService.setNoPrintBoxNoFlag(param);
    }

    /**
     * 查询未打印的箱码
     */
    @PostMapping("/get_noPrint_boxNo")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "user_name", label = "用户名", notNull = true),
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object getNoPrintBoxNo(@RequestBody Map<String, Object> param) throws VtuzxException {
        return ifService.getNoPrintBoxNo(param);
    }

    /**
     * 根据时间和经销商查询销售单据信息
     */
    @PostMapping("/get_sale_by_dealer")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "startTime", label = "开始时间", notNull = true),
            @VtuzxValidator(key = "endTime", label = "结束时间", notNull = true),
            @VtuzxValidator(key = "dealer", label = "经销商", notNull = true),
            @VtuzxValidator(key = "pageIndex", label = "页数", notNull = true)
    } )
    public Object getSaleByDealer(@RequestBody Map<String, Object> param) throws Exception {
        return ifService.getSaleByDealer(param);
    }

    /**
     * 根据销售单号查询明细信息
     */
    @PostMapping("/get_sale_by_tradeNo")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "tradeNo", label = "销售单号", notNull = true)
    } )
    public Object getSaleByTradeNo(@RequestBody Map<String, Object> param) throws Exception {
        return ifService.getSaleByTradeNo(param);
    }

    /**
     * 根据时间查询拆包信息
     */
    @PostMapping("/get_package_by_time")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "startTime", label = "开始时间", notNull = true),
            @VtuzxValidator(key = "endTime", label = "结束时间", notNull = true),
            @VtuzxValidator(key = "pageIndex", label = "页数", notNull = true),
            @VtuzxValidator(key = "state", label = "状态", notNull = true)
    } )
    public Object getPackageByTime(@RequestBody Map<String, Object> param) throws Exception {
        return ifService.getPackageList(param);
    }

    /**
     * 根据销售单号查询包裹信息
     */
    @PostMapping("/get_package_by_tradeNo")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "tradeNo", label = "发货单号", notNull = true)
    } )
    public Object getPackageByOrderNo(@RequestBody Map<String, Object> param) throws Exception {
        return ifService.getPackageByOrderNo(param);
    }

    /**
     * 根据销售单号查询包裹信息
     */
    @PostMapping("/get_inventory_fei_shu")
    @VtuzxApi
    public Object getInventoryFeishu(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.getInventoryFeiShu(token, param);
    }


    /**
     * 绑定追溯链信息(思迅调用)
     */
    @PostMapping("/binding_serial")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "timestamp", label = "时间戳", notNull = true),
            @VtuzxValidator(key = "sign", label = "签名", notNull = true)
    } )
    public Object bindingBerial(@RequestBody Map<String, Object> param) throws Exception {



        return ifService.bindingSerial(param);
    }
}
